const path = require('path')
const { getPluginPathByPluginId } = require('../../common/utils/fileUtil')
const { app, BrowserWindow, globalShortcut, screen, ipcMain } = require('electron');

exports.doCreateIndependentPlugin = function (plugin) {
    createWin(plugin)

}


function createWin(plugin) {
    let mainHtml = plugin.pluginInfo.main
    let preload = plugin.pluginInfo.preload
    preload = preload ? path.join(getPluginPathByPluginId(plugin.pluginId), preload) : null
    mainHtml = mainHtml ? path.join(getPluginPathByPluginId(plugin.pluginId), mainHtml) : null
    const windowOptions = {
        width: 680,
        height: 670,
        titleBarStyle: false,
        frame: true,
        show: false,
        skipTaskbar: false,
        useContentSize: true,
        autoHideMenuBar: false,
        webPreferences: {
            spellcheck: false,
            webSecurity: false,
            enableRemoteModule: true,
            backgroundThrottling: false,
            contextIsolation: true,
            webviewTag: true,
            nodeIntegration: true,
            nodeintegrationinsubframes: true,
            preload: preload
        }

    };
    let mainWindow = new BrowserWindow(windowOptions);
    const theScrean = screen.getDisplayNearestPoint({ x: screen.getCursorScreenPoint().x, y: screen.getCursorScreenPoint().y })
    mainWindow.loadFile(mainHtml);
    mainWindow.getBounds().width

    //接收渲染进程的信息
    mainWindow.on('closed', () => {
        mainWindow = null
    })

    mainWindow.on('blur', () => {
        app.isPackaged && mainWindow.hide()
    })

    mainWindow.once('ready-to-show', () => {
        mainWindow.show()
    })
}